<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="true" label-width="68px">
      <el-form-item label="科室" prop="depId">
        <el-select v-model="queryParams.depId" placeholder="请选择科室">
          <el-option v-for="item in depList" :key="item.deptId" :label="item.deptName" :value="item.deptId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号日期" prop="scheduleDate">
        <el-date-picker clearable v-model="queryParams.scheduleDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择挂号日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="上午，下午" prop="scheduleTime">
        <el-select v-model="queryParams.scheduleTime" placeholder="请选择上午，下午" clearable>
          <el-option label="上午" value="上午"></el-option>
          <el-option label="下午" value="下午"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item label="身份证">
        <el-input v-model="patientCardId" placeholder="请输入身份证号"></el-input>
      </el-form-item>
    </el-form>

    <el-card style="width: 90%; margin: 5px auto;">
      <el-table v-loading="loading" :data="scheduleList" >
      <el-table-column label="科室名称" align="center" prop="depName" />
      <el-table-column label="排班日期" align="center" prop="scheduleDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scheduleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上午，下午" align="center" prop="scheduleTime"></el-table-column>
      <el-table-column label="医生姓名" align="center" prop="userName" />
      <el-table-column label="医生头像" align="center" prop="avatar" />
      <el-table-column label="接诊人数" align="center" prop="scheduleNum" />
      <el-table-column label="剩余名额" align="center" prop="shengyu" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="guahao(scope.row.scheduleId)">挂号</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    </el-card>
    <!-- 建档对话框 -->
    <el-dialog title="建档" :visible.sync="patientDialogFormVisible" style="width: 70%; margin: auto;">
      <el-form :model="patient">
        <el-form-item label="身份证" prop="patientCardId">
          <el-input v-model="patient.patientCardId" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="手机号" prop="patientPhone">
          <el-input v-model="patient.patientPhone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="姓名" prop="patientName">
          <el-input v-model="patient.patientName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="patientSex">
          <el-select v-model="patient.patientSex" placeholder="请选择">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生日" prop="patientBirthday">
          <el-date-picker clearable v-model="patient.patientBirthday" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择排班日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="patient.remark" placeholder="备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="patientDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="insertPatient()">建 档</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { listSchedule } from "@/api/reghos-serv/schedule";
import { listDept } from "@/api/system/dept";
import { goGuahao, addPatient } from '@/api/guahao/guahao';



export default {
  data() {
    return {
      patientDialogFormVisible: false,
      patient: {
        patientPhone: "",
        patientName: "",
        patientSex: "",
        patientCardId: "",
        patientBirthday: "",
        remark: ""
      },
      patientCardId: "",
      total: 0,
      loading: false,
      depList: [],
      scheduleList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        depId: undefined,
        scheduleDate: undefined,
        scheduleTime: undefined,
      },
    };
  },
  methods: {
    getList() {
      this.loading = true;
      listSchedule(this.queryParams).then(response => {
        this.scheduleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.scheduleList = [];
    },
    queryDepList() {
      listDept().then((result) => {
        this.depList = result.data;
      });
    },
    guahao(scheduleId) {
      if (this.patientCardId == "") {
        this.$message.error("请输入身份证号");
        return;
      }
      goGuahao(scheduleId, this.patientCardId).then((result) => {
        if (result.message == "ok") {
          this.$message.success("挂号成功");
          this.handleQuery();
          return;
        }
        if (result.message == "noinfo") {
          this.$message.error("未建档");
          this.patientDialogFormVisible = true;
          this.patient.patientCardId = this.patientCardId;
          return;
        }
        if (result.message == "exist") {
          this.$message.error("存在未完成挂号");
          return;
        }
        this.$message.error("名额已满");
        return;
      });
    },
    insertPatient() {
      addPatient(this.patient).then((result) => {
        if (result.message == "false") {
          this.$message.error("身份证已注册");
          return;
        }
        this.$message.success("建档成功");
        this.patientDialogFormVisible = false;
      });
    }
  },
  created() {
    this.queryDepList();
  }
};
</script>